package io.github.lee.jel.api

import io.github.lee.jel.domain.Item
import io.github.lee.jel.domain.ItemDetail
import io.github.lee.jel.domain.Page
import io.github.lee.jel.domain.Response
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query

interface ItemApi {

    @GET("/Users/{userId}/Items/Resume")
    suspend fun playingList(
        @Path("userId") userId: String,
        @Query("startIndex") startIndex: Int? = null,
        @Query("limit") limit: Int? = null,
        @Query("enableImageTypes") imageTypes: String? = null,
        @Query("searchTerm") searchTerm: String? = null,
        @Query("fields") fields: List<String>? = null,
        @Query("mediaTypes") mediaTypes: List<String>? = null,
    ): Response<Page<Item>>

    @GET("/Users/{userId}/Items/Latest")
    suspend fun latest(
        @Path("userId") userId: String,
        @Query("parentId") parentId: String,
        @Query("startIndex") startIndex: Int? = null,
        @Query("limit") limit: Int? = null,
        @Query("enableImageTypes") imageTypes: String? = null,
    ): Response<List<Item>>


    @GET("/Users/{userId}/Items")
    suspend fun userItems(
        @Path("userId") userId: String,
        @Query("ParentId") parentId: String,
        @Query("StartIndex") startIndex: Int,
        @Query("Limit") size: Int,
        @Query("recursive") recursive: Boolean? = true,
        @Query("includeItemTypes") includeItemTypes: List<String>? = null,
        @Query("filters") filters: List<String>? = null,
        @Query("sortBy") sortBy: List<String>? = null,
        @Query("sortOrder") sortOrder: String? = null,
    ): Response<Page<Item>>


    @GET("/Users/{userId}/Items/{itemId}")
    suspend fun itemDetail(
        @Path("userId")userId:String,
        @Path("itemId")itemId:String,
    ):Response<ItemDetail>

    suspend fun latest(
        userId: String,
        parentId: String?,
        page: Int = 1,
        limit: Int? = 10,
        enableImageTypes: List<QueryImageTypes>? = null,
    ): Response<List<Item>> {
        val startIndex = (page - 1) * (limit ?: 10)
        val sb = StringBuilder()
        enableImageTypes?.forEachIndexed { index, item ->
            sb.append(item.toString())
            if (index != enableImageTypes.size - 1) {
                sb.append(",")
            }
        }
        return ApiModule.itemApi()
            .latest(userId, parentId ?: "", startIndex, limit, sb.toString())
    }

}

        